//
//  JQVisitorView.swift
//  sinaWeibo
//
//  Created by maoge on 16/4/16.
//  Copyright © 2016年 maoge. All rights reserved.
//

import UIKit
import SnapKit

protocol JQVisitorViewDelegate: NSObjectProtocol {
    func loginBtnCLick()
    func registerBtnClick()
    
    
//    optional func haha()
}

class JQVisitorView: UIView {
    
    //设置delegate
    weak var delegate: JQVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    //设置当前访客视图内容
    func setVisitorInfo(imageNamed: String?, message: String?){
        
        if let imageN = imageNamed, msg = message {
            iconView.image = UIImage(named: imageN)
            messageLabel.text = msg

        }else {
            startAnimation()
        }
    }
    
    //开始动画
    private func startAnimation() {
        circleView.hidden = false
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //1, 添加控件
        addSubview(iconView)
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(circleView)
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(messageLabel)
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(registerButton)
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(loginButton)
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(maskIconView)
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
        //2, 设置约束
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        //圆环约束
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        //文字消息约束
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
//        // 添加最大宽度约束
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(15)
            make.width.equalTo(224)
        }
        
        //注册按钮约束
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.width.equalTo(100)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
        }
        
        //登录按钮约束
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.width.equalTo(100)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
        }
        
        //添加半透明遮罩
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.centerY.equalTo(circleView.snp_centerY)
        }
    }
    
    
    //大房子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //圆环
    private lazy var circleView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        image.hidden = true
        
        return image
        
    }()
    
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14, preferredMaxLayoutWidth: 215)
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        // 文本对齐方式
        label.textAlignment = .Center
        
        return label
    }()
    
    //登录按钮
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        btn.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
        
        return btn
        
    }()
    
    //注册按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)

        
        return btn
        
    }()
    
    //半透明背景
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //实现点击监听
    
    @objc private func registerButtonClick() {
        delegate?.registerBtnClick()
    }
    
    @objc private func loginButtonClick() {
        delegate?.loginBtnCLick()
    }
}

extension JQVisitorView {
    
}
